<?php
/**
 * Created by PhpStorm.
 * User: user1
 * Date: 16-10-22
 * Time: 下午1:24
 */

namespace Admin\Controller;

use Think\Controller;
use Think\Exception;

vendor('alidayu');
vendor('alidayu.TopSdk');
vendor('alidayu.top.request.AlibabaAliqinFcSmsNumSendRequest');

class PublicController extends Controller
{
    public function upload()
    {
        $upload = $this->getUploadObj();
        $info = $upload->upload();
        if (!$info) {// 上传错误提示错误信息
            $this->ajaxReturn(array('success' => false, 'message' => '上传失败' . $upload->getError()));
        } else {// 上传成功
            $url = C('SAVEROOT') . $info['file']['savepath'] . $info['file']['savename'];
            $this->ajaxReturn(array('success' => true, 'message' => '上传成功', 'path' => $url));
        }

    }


    private function getUploadObj()
    {

        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Uploads/'; // 设置附件上传根目录
        $upload->savePath = 'Images/'; // 设置附件上传（子）目录
        if (!is_dir('./Uploads/')) {
            mkdir('./Uploads/', 0755, true);
        }
        // 上传文件
        return $upload;
    }

    /**
     *根据父城市id获取下级所有城市
     * @param int $parent_id 　父城市id
     */
    public function getRegionListByParent($parent_id = 0)
    {
        $map['parent_id'] = ':parent_id';
        $bind[':parent_id'] = $parent_id;
        $list = D('Region')->where($map)->bind($bind)->select();
        $this->ajaxReturn($list);
    }

    /**
     * 发送短信
     * @param $number 手机号码
     * @return mixed|\ResultSet|\SimpleXMLElement
     */
    public function sendSMS($number)
    {
        $file = 'pay.txt';
        $fp = fopen($file, 'a+');
        flock($fp, LOCK_EX);
        if (!isMobile($number)) {
            flock($fp, LOCK_UN);
            fclose($fp);
            $this->ajaxReturn(array('success' => false, 'message' => '请输入正确的手机号'));
        }
        $create_time = session('create_time');
        $time = C('SMS_TIME');
        if (!empty($create_time) && time() - $create_time < $time) {
            flock($fp, LOCK_UN);
            fclose($fp);
            $this->ajaxReturn(array('success' => false, 'message' => '不能重复获取'));
        }
        date_default_timezone_set('Asia/Shanghai');
        $appkey = C('APP_KEY');
        $secret = C('APP_SECRET');
        $templateCode = C('TEMPLATE_CODE');
        $signName = C('SIGN_NAME');
        $c = new \TopClient;
        $c->appkey = $appkey;
        $c->secretKey = $secret;
        $req = new \AlibabaAliqinFcSmsNumSendRequest;
//        $req ->setExtend( "" );
        $req->setSmsType("normal");
        $req->setSmsFreeSignName($signName);
        $code = rand(100000, 999999);
        $req->setSmsParam("{\"code\":\"" . $code . "\"}");
        $req->setRecNum($number);
        $req->setSmsTemplateCode($templateCode);
        $resp = $c->execute($req);
        session('number', $number);
        session('code', $code);
        session('create_time', time());
        $resp = json_decode($resp);
        flock($fp, LOCK_UN);
        fclose($fp);
        $this->ajaxReturn(array('success' => true));
    }

    function SHA256Hex($str){
        $re=hash('sha256', $str, true);
        return bin2hex($re);
    }

    public function test(){
        $password = md5('123456' . C('MD5_KEY'));

    }

//    public function test(){
//        exit('测试12345678987654212345678901');
//    }

//    public function test1(){
//        $list = D('MatchLog')->join('LEFT JOIN t_room r ON r.id=t_match_log.room_id')
//            ->field('t_match_log.*,r.model')->select();
//        $model = M();
//        $model->startTrans();
//        try {
//            foreach ($list as $key => $value) {
//                $list[$key]['model'] = $value['model'];
//                $model->table('t_match_log')->where(array('id'=>$value['id']))->setField('model',$value['model']);
//            }
//            $model->commit();
//        }catch (Exception $e){
//            $model->rollback();
//        }
//    }
}